
import { Controller, Get, Post, Body, Patch, Param, Delete, Res, Header, StreamableFile, UseInterceptors, UploadedFile, UploadedFiles } from '@nestjs/common';
import { FileoperationService } from './fileoperation.service';
import { CreateFileoperationDto } from './dto/create-fileoperation.dto';
import { UpdateFileoperationDto } from './dto/update-fileoperation.dto';
import { LogService } from '../log/log.service';

import { createReadStream } from 'fs';
import { join } from 'path';
import type { Response } from 'express'; // Assuming that we are using the ExpressJS HTTP Adapter
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';



@Controller('file')
export class FileoperationController {
  constructor(private readonly fileoperationService: FileoperationService,
    private readonly logService: LogService
  ) { }

  // 流方传输
  @Get()
  @Header('Content-Type', 'application/json')
  @Header('Content-Disposition', 'attachment; filename=".env"')
  getFileUsingStaticValues(): StreamableFile {
    const file = createReadStream(join(process.cwd(), '.env'));
    return new StreamableFile(file);
  }


  // 文件列表
  // @Post('list')
  // fileList(@Body() name: any, @Body() pageNum: number) {
  //   this.logService.logVerbose('file', 'fileList执行');
  //   return this.fileoperationService.fileList(name, pageNum);
  // }
  @Get('list')
  async getFileList(@Body() body: { name?: string; pageNum: number }) {
    const { name, pageNum } = body;
    return this.fileoperationService.fileList(name, pageNum);
  }
  // 查看存储空间列表
  @Get('space')
  listBuckets() {
    this.logService.logVerbose('file', 'listBuckets执行')
    return this.fileoperationService.listBuckets();
  }
  // 单上传文件 √
  // @Post('standard')
  // @UseInterceptors(FileInterceptor('file'))
  // async uploadFile(@UploadedFile() file) {
  //   console.log("进入文件标准上传");

  //   const res = this.fileoperationService.uploadFile(file)
  //   return res;
  // }
  // 多文件上传
  // @Post()
  // @UseInterceptors(FilesInterceptor('files')) // 注意这里使用的是FilesInterceptor
  // async uploadFiles(@UploadedFiles() files: Express.Multer.File[], @Body('filenames') filenames?: string[]) {
  //   try {
  //     if (!filenames || filenames.length !== files.length) {
  //       throw new Error('Filenames array must match the number of uploaded files.');
  //     }

  //     const results = [];
  //     for (let i = 0; i < files.length; i++) {
  //       const file = files[i];
  //       const filename = filenames[i] || file.originalname; // 使用提供的文件名或原始文件名
  //       const result = await this.fileoperationService.uploadFiles(file.buffer, filename);
  //       results.push(result);
  //     }

  //     return results;
  //   } catch (e) {
  //     console.error('Error uploading files:', e);
  //     throw e;
  //   }
  // }


  // 多文件上传 √
  @Post('standard')
  @UseInterceptors(FilesInterceptor('files'))
  async uploadMultipleFiles(@UploadedFiles() files: Express.Multer.File[]) {
    try {
      const results = await this.fileoperationService.uploadMultipleFiles(files, 1);
      return results;
    } catch (e) {
      console.error('Error uploading multiple files:', e);
      throw e;
    }
  }
  // 多文件上传（论文查询）
  @Post('paper')
  @UseInterceptors(FilesInterceptor('files'))
  async uploadMultipleFiles2(@UploadedFiles() files: Express.Multer.File[]) {
    try {
      const results = await this.fileoperationService.uploadMultipleFiles(files, 2);
      return results;
    } catch (e) {
      console.error('Error uploading multiple files:', e);
      throw e;
    }
  }


  // 下载文件
  @Get('download')
  get() {
    this.logService.logVerbose('file', 'get执行')
    return this.fileoperationService.get();
  }
  // 删除文件
  @Get('delete')
  deleteObject(@Body() body: any) {
    this.logService.logVerbose('file', 'deleteFile执行')
    return this.fileoperationService.deleteObject();
  }

  @Get('l')
  logVerbose() {
    this.logService.logVerbose('file', 'logVerbose')


  }

}
